package com.woniu.venue.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.venueInfo.VenueInfoException;
import com.woniu.exception.venueInfo.VenueInfoExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.JwtUtil;
import com.woniu.util.RedisUtil;
import com.woniu.util.WebUtil;
import com.woniu.venue.client.model.VenueAdminDto;
import com.woniu.venue.dto.venueInfo.VenueInfoDto;
import com.woniu.venue.form.venueInfo.AddVenueInfoForm;
import com.woniu.venue.form.venueInfo.UpdateVenueInfoForm;
import com.woniu.venue.form.venueInfo.VenueInfoLoadFrom;
import com.woniu.venue.model.VenueInfo;
import com.woniu.venue.param.venueInfo.AddVenueInfoParam;
import com.woniu.venue.param.venueInfo.VenueInfoLoadParam;
import com.woniu.venue.service.VenueInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */

@Api(tags = "场馆信息")
@RestController
@RequestMapping("/venueInfo")
public class VenueInfoController {
    @Autowired
    private VenueInfoService venueInfoService;
    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation("加载所有场馆信息")
    @PostMapping("/loadVenueInfos")
    public Result load(@RequestBody VenueInfoLoadFrom from)throws Exception{
        PageInfo<VenueInfoDto> venueInfoDtoPageInfo=venueInfoService.load(BeanUtil.toBean(from, VenueInfoLoadParam.class));
        return Result.getResult().setData(venueInfoDtoPageInfo).setMsg("查询成功");
    }

    @ApiOperation("修改场馆信息的状态")
    @GetMapping("/changeState/{venueInfoId}/{state}")
    public Result changeState(@PathVariable(value = "venueInfoId") Integer venueInfoId,@PathVariable("state") Integer state) throws Exception {
        String token = WebUtil.getToken();
        String refreshToken = redisUtil.get(token);
//        VenueInfoDto venueInfo = (VenueInfoDto) JwtUtil.getVenueInfo(refreshToken);
//        if (venueInfo.getVenueInfoId()== venueInfoId) throw new VenueInfoException(VenueInfoExceptionCode.VENUE_ADMIN_CANNOT_CHANGE);
        venueInfoService.changeState(venueInfoId,state);
        return Result.getResult().setMsg("修改状态成功");
    }

    @ApiOperation("新增场馆信息")
    @PostMapping("/addVenueInfo")
    public Result addVenueInfoAccount(@RequestBody AddVenueInfoForm form) throws Exception {
        VenueInfoDto venueInfoDto= venueInfoService.addVenueInfo(BeanUtil.toBean(form, AddVenueInfoParam.class));
        return Result.getResult().setData(venueInfoDto).setMsg("新增场馆信息成功");
    }

    @ApiOperation("修改场馆信息信息")
    @PostMapping("/updateVenueInfo")
    public Result updateVenueInfoAccount(@RequestBody UpdateVenueInfoForm form) throws Exception {
        boolean b = venueInfoService.updateById(BeanUtil.toBean(form, VenueInfo.class));
        if (!b) throw new VenueInfoException(VenueInfoExceptionCode.VENUE_INFO_ADD_FAIL);
        VenueInfoDto venueInfoDto = BeanUtil.toBean(venueInfoService.getById(form.getVenueId()), VenueInfoDto.class);
        return Result.getResult().setData(venueInfoDto).setMsg("修改成功");
    }


    @ApiOperation("加载场馆信息自己的信息")
    @PostMapping("/loadVenueInfo")
    public Result loadAccount() throws Exception {
        String token = WebUtil.getToken();
        String s = redisUtil.get(token);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(s)),VenueAdminDto.class);
        VenueInfo venueInfo = venueInfoService.getById(venueAdminDto.getVenueId());
        if (ObjectUtil.isEmpty(venueInfo)) throw new VenueInfoException(VenueInfoExceptionCode.VENUE_INFO_NOT_EXIST);
        VenueInfoDto venueInfoDto = BeanUtil.toBean(venueInfo, VenueInfoDto.class);
        return Result.getResult().setData(venueInfoDto).setMsg("查询成功");
    }

    @ApiOperation("根据场馆id查询场馆信息")
    @GetMapping("/loadVenueInfoById/{id}")
    public Result loadVenueInfoById(@PathVariable("id") Integer id){
        VenueInfo byId = venueInfoService.getById(id);
        return Result.getResult().setData(byId).setMsg("成功");
    }

}

